package com.sinaapp.gavinzhang.ConcurrentProgramming;

import java.util.Random;
import java.util.concurrent.*;

/**
 * CountDownLatch只能用一次，构造方法的值是先行完成countDown方法的次数
 * Created by gavin on 15-9-1.
 */
public class CountDownLatchTest {
    public static void main(String[] args)
    {
        CountDownLatch countDownLatch = new CountDownLatch(5);
        ExecutorService threadPoolExecutor = new ThreadPoolExecutor(5,5,1000L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
        for (int i = 0; i <5 ; i++) {
            threadPoolExecutor.execute(new Participant(countDownLatch));
        }

    }
}


class Participant implements Runnable{
    private CountDownLatch countDownLatch;

    public void run() {
        try {
            Thread.sleep(new Random().nextInt(6000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"  arrive");
        countDownLatch.countDown();
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName()+"  到齐了!!!");

    }

    public Participant(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }
}
